package com.tian.controller;

import com.github.pagehelper.PageInfo;
import com.tian.entity.Echarts01;
import com.tian.entity.Emp;
import com.tian.entity.User;
import com.tian.service.EmpService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Tian
 * @version: 1.0
 * @since: 1.0
 * @date: 2024/3/8/20:55
 */
@Controller
public class EmpController {

    @Resource
    private EmpService empServiceImpl;

    @RequestMapping(value = "/add")
    public String addEmp(Emp emp){
        int i = empServiceImpl.insertEmp(emp);
        if(i==1){
            return "redirect:getEmps.jsp";
        }else {
            return "redirect:addEmp.js";
        }
    }


    @RequestMapping(value = "/getEmp")
    public String selectEmp(Long id, Model model){
        Emp emp = empServiceImpl.selectEmp(id);
        model.addAttribute("e",emp);
        return "modifyEmp.jsp";
    }


    @RequestMapping(value = "/updateEmp")
    public  String updateEmp(Emp emp){
        int count = empServiceImpl.updateEmp(emp);
        if(count==1){
            return "redirect:getEmps";
        }else {
            return "/getEmp?id"+emp.getEmpno();
        }
    }

    @RequestMapping(value = "/getEmps")
    public String selectAll(@RequestParam(defaultValue = "1") int current, @RequestParam(defaultValue = "5") int pageSize, Model model){
        PageInfo<Emp> pageInfo=empServiceImpl.selectAll(current,pageSize);
        System.out.println(pageInfo);
        model.addAttribute("pageInfo",pageInfo);
        return "/emplist.jsp";
    }

    @GetMapping(value = "/getAxios")
    @ResponseBody
    public  Emp getAxios(Integer id){
        System.out.println(id);
        Emp emp = empServiceImpl.selectEmp(7521L);
        return emp;
    }

    @PostMapping(value = "/getPostAxios")
    @ResponseBody
    public  Emp getPostAxios(@RequestBody  Emp emp){
        return emp;
    }


    @GetMapping(value = "/echart/demo01")
    @ResponseBody
    public List<Echarts01> getPostAxios(){
        List<Echarts01> echarts01s = empServiceImpl.selectCount();
        System.out.println("---------------------");
        System.out.println(echarts01s);
        return echarts01s;
    }

}
